/*
 * @(#) JobEvaEntryServiceImpl.java Sep 14, 2011
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.jobeva.business.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.strong.gweva.common.business.entity.db.JxkhJobRuleEntity;
import cn.strong.gweva.common.business.entity.db.JxkhResultEntity;
import cn.strong.gweva.common.business.service.impl.AbstractServiceImpl;
import cn.strong.gweva.common.util.Constants;
import cn.strong.gweva.common.util.Constants4Method;
import cn.strong.gweva.common.util.DateUtil;
import cn.strong.gweva.jobeva.business.entity.JobEvaEntryEntity;
import cn.strong.gweva.jobeva.business.entity.JobRuleExEntity;
import cn.strong.gweva.jobeva.business.service.JobEvaEntryService;


/**
 * 岗位考核录入service实现类.
 * 
 * @author jincong
 * @version 1.0
 * @since Sep 14, 2011
 */
public class JobEvaEntryServiceImpl extends AbstractServiceImpl implements JobEvaEntryService
{

	/**
	 * 根据岗位考核项目ID查询岗位考核规则列表.
	 * 
	 * @param itemId 岗位考核项目ID
	 * @return 岗位考核规则列表
	 * @throws Exception 异常
	 */
	@SuppressWarnings("unchecked")
	public List<JobRuleExEntity> searchJobRuleListByItemId(Integer itemId) throws Exception
	{
		return this.getCommonDao().searchList(
				Constants4Method.JOBEVA_SEARCH_JOB_RULE_BY_ITEM_ID, itemId);
	}

	/**
	 * 录入岗位考核.
	 * 
	 * @param jobEvaEntryEntity 岗位考核录入entity
	 * @throws Exception 异常
	 */
	public void save(JobEvaEntryEntity jobEvaEntryEntity) throws Exception
	{
		if (jobEvaEntryEntity.getJobRuleList() != null
				&& jobEvaEntryEntity.getJobRuleList().size() > 0)
		{
			List<JxkhResultEntity> resultList = new ArrayList<JxkhResultEntity>();

			// 考核规则
			for (JxkhJobRuleEntity rule : jobEvaEntryEntity.getJobRuleList())
			{

				JxkhResultEntity result = new JxkhResultEntity();
				// 工作人员编号
				result.setWorkerCode(rule.getWorkerCode());
				// 分数
				if (Constants.SCORING_WAY_NUMBER.equals(rule.getScoringWay()))
				{
					result.setScore(rule.getScore());
				}
				else if (Constants.SCORING_WAY_PERCENT.equals(rule.getScoringWay()))
				{
					result.setScore(jobEvaEntryEntity.getScore().multiply(rule.getScore()).divide(
							new BigDecimal(100), 2, RoundingMode.HALF_UP));
				}

				if (result.getScore() == null)
				{
					result.setScore(BigDecimal.ZERO);
				}

				// 根据次数计算分数
				if (jobEvaEntryEntity.getCount() != null)
				{
					result.setScore(result.getScore()
							.multiply(new BigDecimal(jobEvaEntryEntity.getCount())));
				}

				// 考核规则ID
				result.setRuleId(rule.getRuleId());
				// 考核类型
				result.setType(Constants.EVA_TYPE_JOB);
				// 考核时间
				result.setEvaTime(jobEvaEntryEntity.getEvaTime());
				// 是否是总动计算结果
				result.setIsAuto(Constants.IS_AUTO_NO);
				// 备注
//				result.setRemark("规则名称：" + rule.getRuleName()
//						+ System.getProperty("line.separator") + "规则备注：" + rule.getRemark()
//						+ System.getProperty("line.separator") + "录入备注："
//						+ jobEvaEntryEntity.getRemark() + System.getProperty("line.separator")
//						+ "录入时间：" + DateUtil.formatDate(new Date(), DateUtil.DATE_PATTERN_18));
				result.setRemark(jobEvaEntryEntity.getRemark());
				// 是否删除
				result.setDelFlag(Constants.DEL_FLAG_NO);
				// 更新时间
				result.setUpdateTime(new Date());
				// 更新用户ID
				result.setUpdateUserId(jobEvaEntryEntity.getUpdateUserId());

				resultList.add(result);
			}

			this.getCommonDao().insertBatch(Constants4Method.JOBEVA_INSERT_JXKH_RESULT, resultList);
		}
	}
}
